package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.framework.commons.utils.EmptyUtil;
import com.itheima.sfbx.insurance.dto.InsuranceConditionVO;
import com.itheima.sfbx.insurance.enums.InsuranceConditionEnum;
import com.itheima.sfbx.insurance.mapper.InsuranceConditionMapper;
import com.itheima.sfbx.insurance.pojo.InsuranceCondition;
import com.itheima.sfbx.insurance.service.IInsuranceConditionService;
import com.itheima.sfbx.insurance.service.IInsuranceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description：保险筛选项服务实现类
 */
@Slf4j
@Service
public class InsuranceConditionServiceImpl extends ServiceImpl<InsuranceConditionMapper, InsuranceCondition> implements IInsuranceConditionService {

    @Autowired
    private IInsuranceService insuranceService;

    private QueryWrapper<InsuranceCondition> queryWrapper(InsuranceConditionVO insuranceConditionVO){
        QueryWrapper<InsuranceCondition> queryWrapper = new QueryWrapper<>();
        //保险商品id查询
        if (!EmptyUtil.isNullOrEmpty(insuranceConditionVO.getInsuranceId())) {
            queryWrapper.lambda().eq(InsuranceCondition::getInsuranceId,insuranceConditionVO.getInsuranceId());
        }
        //保险商品条件key查询
        if (!EmptyUtil.isNullOrEmpty(insuranceConditionVO.getConditionKey())) {
            queryWrapper.lambda().eq(InsuranceCondition::getInsuranceId,insuranceConditionVO.getConditionKey());
        }
        //条件项名称查询
        if (!EmptyUtil.isNullOrEmpty(insuranceConditionVO.getConditionKeyName())) {
            queryWrapper.lambda().like(InsuranceCondition::getConditionKeyName,insuranceConditionVO.getConditionKeyName());
        }
        //条件想值查询
        if (!EmptyUtil.isNullOrEmpty(insuranceConditionVO.getConditionVal())) {
            queryWrapper.lambda().eq(InsuranceCondition::getConditionVal,insuranceConditionVO.getConditionVal());
        }
        //备注查询
        if (!EmptyUtil.isNullOrEmpty(insuranceConditionVO.getRemake())) {
            queryWrapper.lambda().like(InsuranceCondition::getRemake,insuranceConditionVO.getRemake());
        }
        //状态查询
        if (!EmptyUtil.isNullOrEmpty(insuranceConditionVO.getDataState())) {
            queryWrapper.lambda().eq(InsuranceCondition::getDataState,insuranceConditionVO.getDataState());
        }
        //按创建sortNo排序
        queryWrapper.lambda().orderByAsc(InsuranceCondition::getSortNo);
        return queryWrapper;
    }

    @Override
    @Transactional
    public Boolean save(List<InsuranceConditionVO> insuranceConditionListVOs) {
        //新增条件
        //由于全部字段的数据前端都有，直接存
        //转po
        List<InsuranceCondition> beanList = BeanConv.toBeanList(insuranceConditionListVOs, InsuranceCondition.class);
        return this.saveBatch(beanList);
    }

    @Override
    @Transactional
    public Boolean update(List<InsuranceConditionVO> insuranceConditionVOs) {
        if(CollUtil.isEmpty(insuranceConditionVOs)){
            throw new ProjectException(InsuranceConditionEnum.UPDATE_FAIL);
        }
        //先删
        String insuranceId = insuranceConditionVOs.get(0).getInsuranceId();
        LambdaQueryWrapper<InsuranceCondition> qw = new LambdaQueryWrapper<InsuranceCondition>().eq(InsuranceCondition::getInsuranceId, insuranceId);
        int count = this.count(qw);
        if(count>0){
            this.remove(qw);
        }
        //再加
        return this.save(insuranceConditionVOs);
    }


    @Override
    public List<InsuranceConditionVO> findList(InsuranceConditionVO insuranceConditionVO) {
        //查询列表
        QueryWrapper<InsuranceCondition> qw = this.queryWrapper(insuranceConditionVO);
        List<InsuranceCondition> list = this.list(qw);
        if(CollUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        List<InsuranceConditionVO> beanList = BeanConv.toBeanList(list, InsuranceConditionVO.class);
        return beanList;
    }
}
